import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import $axios from '../tools/axios'
// import {useRouter} from 'vue-router'

// const router = useRouter();

const basepath = '/user'
export const useLoginStore = defineStore('login', {
  state: () => {
    return {
    
    }
  },
  actions: {
    async login(val) {
      const path = basepath + '/login'

      const { data: res } = await $axios.post(path, val)

      if (res.code === 200) {
        
        sessionStorage.setItem('name', res.data.name)
        sessionStorage.setItem('user', JSON.stringify(res.data))
        ElMessage({
          message: '登录成功！',
          type: 'success'
        })
        return 1
      } else {
        ElMessage.error('用户名或密码错误！')
        return 0
      }
    }
  }
})
